<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: Controller.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Kowcore_Widget_FeaturedContestsController extends Engine_Content_Widget_Abstract
{
  public function indexAction()
  {
    if (!Engine_Api::_()->getDbTable('modules', 'core')->isModuleEnabled('contest')) {
      return $this->setNoRender();
    }

    $type = 'contest';
    $featuredTable = Engine_Api::_()->getDbTable('featured', 'kowcore');
    $table = Engine_Api::_()->getItemTable($type);
    
    $name = $table->info('name');
    $fName = $featuredTable->info('name');
    $viewer = Engine_Api::_()->user()->getViewer();
    $primary = array_shift($table->info('primary'));

    $select = $featuredTable->select()
      ->setIntegrityCheck(false)
      ->from($fName, array('id'))
      ->where('type = ?', $type);
    $ids = $featuredTable->getAdapter()->fetchCol($select);

    if (empty($ids)) {
      return $this->setNoRender();
    }
    
    $select = $table->select()
      ->where($primary.' IN (?)', $ids);

    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage(9);
    
    if (!$paginator->getTotalItemCount()) {
      return $this->setNoRender();
    }
  }
}